package com.qf.j2102.controller;


import com.qf.j2102.common.JsonBean;
import com.qf.j2102.po.SysUser;
import com.qf.j2102.service.SysUserService;
import com.qf.j2102.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService userService;

    @RequestMapping("/login.do")
    @ResponseBody
    public JsonBean login(String tel, String password, HttpSession session) {
        JsonBean bean = null;

        try {
            SysUser users = userService.findByName(tel, password);
            session.setAttribute("user",users);
            bean = JsonUtils.createJsonBean(1,users);

        }catch (Exception e) {
            e.printStackTrace();
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }


        return bean;

    }

    @RequestMapping("/register.do")
    @ResponseBody
    public JsonBean regiest(SysUser user){
        JsonBean bean = null;
        try {
            userService.addUser(user);
            bean = JsonUtils.createJsonBean(1,null);

        }catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }

        return bean;
    }


    @RequestMapping("/find.do")
    @ResponseBody
    public JsonBean findUser(Integer id) {
        JsonBean bean = null;

        try {
            SysUser user = userService.findUserById(id);
            bean = JsonUtils.createJsonBean(1,user);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }
        return bean;

    }

    @RequestMapping("/denglu.do")
    @ResponseBody
    public JsonBean findUser(HttpSession session) {
        JsonBean bean = null;

        SysUser user1 = (SysUser) session.getAttribute("user");
        if (user1 == null) {
            bean = JsonUtils.createJsonBean(0,0);
        } else {
            bean = JsonUtils.createJsonBean(1,user1);
        }

        return bean;

    }


    @RequestMapping("/update.do")
    @ResponseBody
    public JsonBean updateTel(HttpSession session,int id, String tel,String email,String password,String invitation) {
        JsonBean bean = null;
        SysUser user1 = new SysUser(id,tel,password,email,invitation);
        try {
            userService.update(user1);
            bean = JsonUtils.createJsonBean(1, "修改成功！");
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0, e.getMessage());
        }
        return bean;
    }
     @RequestMapping("/update1.do")
     @ResponseBody
     public JsonBean updateTel1(HttpSession session,String email){
        JsonBean bean = null;
        SysUser user1 = (SysUser) session.getAttribute("user");
        user1.setEmail(email);
        try {
          userService.update(user1);
           bean = JsonUtils.createJsonBean(1, "修改成功！");
           } catch (Exception e) {
                bean = JsonUtils.createJsonBean(0, e.getMessage());
        }
        return bean;
    }
    @RequestMapping("/update2.do")
    @ResponseBody
    public JsonBean updateTel2(HttpSession session, String password){
        JsonBean bean = null;
        SysUser user1 = (SysUser) session.getAttribute("user");
        user1.setPassword(password);
        try {
            userService.update(user1);
            bean = JsonUtils.createJsonBean(1, "修改成功！");
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0, e.getMessage());
        }


        return bean;

    }
    @RequestMapping("/update3.do")
    @ResponseBody
    public JsonBean updateTel3(HttpSession session, String tel){
        JsonBean bean = null;
        SysUser user1 = (SysUser) session.getAttribute("user");
        user1.setTel(tel);
        try {
            userService.update(user1);
            bean = JsonUtils.createJsonBean(1, "修改成功！");
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0, e.getMessage());
        }


        return bean;

    }
    @RequestMapping("/del.do")
    @ResponseBody
    public JsonBean del1(int  id){
        JsonBean bean = null;
        try {
            userService.del(id);
            bean = JsonUtils.createJsonBean(1,null);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }
        return bean;

    }


}
